<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
// namespace Admin\Controller;
namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class OrderController extends HomeController {
	public function index(){
		$oid=I('oid');
		$uid = $_SESSION['onethink_home']['user_auth']['uid'];
		$order=M('order')->where(array('id'=>array('in',$oid)))->select();
		$count=M('order')->where(array('id'=>array('in',$oid)))->count();
		$sumprice=M('order')->where(array('id'=>array('in',$oid)))->sum('sumprice');
		$addr=M('address')->where(array('uid'=>$uid))->select();
		foreach ($order as $k => $v) {
			$goods=M('goods')->where(array('id'=>$v['gid']))->field('id,icon,name')->find();
            $attrval=M('attrval')->where(array('id'=>array('in',$v['attrvid'])))->field('value')->select();
            $path = M('picture')->where(array('id'=>$goods['icon']))->field('path')->find();
            $order[$k]['gname']=$goods['name'];
            $order[$k]['path']=$path['path'];
            $order[$k]['attrval']=implode(',',array_column($attrval,'value'));
		}
		// var_dump($addr);
		$this->assign('addr',$addr);
		$this->assign('order',$order);
		$this->assign('count',$count);
		$this->assign('sumprice',$sumprice);
		$this->display();
	}
	public function address(){

       $_POST['uid']=$_SESSION['onethink_home']['user_auth']['uid'];
       $res=M('address')->add($_POST);
       if ($res) {
       	$data['info']='添加成功';
       	$data['status'] = 1;
       }else{
       	$data['info']='添加失败';
       	$data['status'] = 0;
       }
       $this->ajaxReturn;
	}
	public function order(){
		// //库存减
  //   foreach ($_POST['attrvid'] as $k => $v) {

  //     $kucun = M('price')->where(array('attrvalid'=>$v))->setDec('num',$_POST['gnum'][$k]);

  //   }

  //   //销量加
  //    foreach ($_POST['gid'] as $ke => $va) {

  //     $sale = M('goods')->where(array('id'=>$va))->setInc('sum',$_POST['gnum'][$ke]);

  //   }
		$data['dingdanhao']=date('YmdHis').mt_rand(10,20);
		$data['time']=time();
		$data['status']=1;
		$data['aid']=$_POST['aid'];
		$map['id']=array('in',$_POST['oid']);
		$res=M('Order')->where($map)->save($data);
		if ($res) {
			$data['info']='下单成功';
			$data['status']=1;
			$data['dingdanhao'] = $data['dingdanhao'];
		}else{
			$data['info']='下单失败';
			$data['status']=0;
		}$this->ajaxReturn($data);
	}
	public function pay(){
		$dingdanhao=I('get.dingdanhao');
		// var_dump($dingdanhao);
		$sumprice=M('order')->where(array('dingdanhao'=>$dingdanhao))->sum('sumprice');

		// var_dump($sumprice);
		$this->assign('sumprice',$sumprice);
		$this->assign('dingdanhao',$dingdanhao);
		$this->display();
	}
	
}